<?
    class categoryFilterManufacturers extends CWidget {

		public $avalible_items;
		public $requested_manufacturers;
		public $feature;
		public $id;
		public $perpage;
		public $order;
		public $beforeText = 'Производители';
		public $criteria;

        public function run() {
			$avalible_manufacturers = array();
			foreach($this->avalible_items as $item){
				$avalible_manufacturers[] = $item['manufacturer'];
			}
			$avalible_manufacturers = array_diff($avalible_manufacturers,array(''));
			if(isset($avalible_manufacturers) AND count($avalible_manufacturers) > 0){
				$avalible_manufacturers = array_diff($avalible_manufacturers,array(''));
			}else{
				$avalible_manufacturers = array();
			}

			$avalible_manufacturers = array_unique($avalible_manufacturers);
			
			echo $this->beforeText;
			
			foreach($avalible_manufacturers as $avalible_manufacturer){

				$criteria = new CDbCriteria();
				$criteria->compare('category',$this->id);
				$criteria->compare('manufacturer',$avalible_manufacturer);
				$count = Item::model()->count($criteria);

				$avalible_manufacturer = Manufacturer::model()->findByPk($avalible_manufacturer);
				if(!in_array($avalible_manufacturer->id, $this->requested_manufacturers)){
					$manufacturer_link = $this->requested_manufacturers;
					$manufacturer_link[] = $avalible_manufacturer->id;

					$manufacturer_title = $avalible_manufacturer->title;
				}else{
					$manufacturer_link = $this->requested_manufacturers;
					$manufacturer_link = array_flip($manufacturer_link);
					unset($manufacturer_link[$avalible_manufacturer->id]);
					$manufacturer_link = array_flip($manufacturer_link);

					$manufacturer_title = '<b>'.$avalible_manufacturer->title.'</b>';
				}
				if((count($this->requested_manufacturers) > 0) AND (count($this->requested_manufacturers) != (count($avalible_manufacturers))) AND (!in_array($avalible_manufacturer->id,$this->requested_manufacturers))){
					$plus = '+';
				}else{
					$plus = '';
				}
				if($count > 0){
					$manufacturer_title = $manufacturer_title.'('.$plus.$count.')';
				}
				echo "&nbsp;";
				echo CHtml::link($manufacturer_title, Yii::app()->createurl('category/index', array(
					'id' => $this->id,
					'manufacturer' => $manufacturer_link,
					'feature' => $this->feature,
					'perpage' => $this->perpage,
					'order' => $this->order
				)));
			}
        }
    }
?>